function onMapsClick(param)
{
    if (!geoName) {
        jQuery("input[name='geo_name']").val('');
        jQuery("input[name='national_id']").val('');
        jQuery("input[name='geo_level']").val('');
    } else {
        var geoLevel = getGeoLevel(geoName);
        var nationalID = getNationalID(geoLevel, geoName);
        jQuery("input[name='geo_name']").val(geoName);
        jQuery("input[name='national_id']").val(nationalID);
        jQuery("input[name='geo_level']").val(geoLevel);
    }
}

/*******************************画面相关************************************/
function getDistributorMonthlyReportStat(){
    jQuery.ajaxSettings.async = false;
    var params = [];
    var geoLevel = getGeoLevel(geoName);
    var nationalID = getNationalID(geoLevel, geoName);

    params.push('parameters[geo_name]=' + (!geoName ? geoChain[geoChain.length-1] : geoName));
    params.push('parameters[geo_level]=' + geoLevel);
    params.push('parameters[national_id]=' + nationalID);
    params.push('parameters[year]=' + jQuery("select[name='year']").val());
    params.push('parameters[month]=' + jQuery("select[name='month']").val());

    jQuery.getJSON(nonghuaBaseUrl + '/api/sales_monthly_report?' + params.join('&'), function (json) {
        optionThisMonthDelivery.xAxis[0].data = json.thisMonthDelivery.times;
        optionThisMonthDelivery.series[0].data = json.thisMonthDelivery.values;
        barChartThisMonthDelivery.setOption(optionThisMonthDelivery, true);

        optionThisMonthSales.xAxis[0].data = json.thisMonthSales.times;
        optionThisMonthSales.series[0].data = json.thisMonthSales.values;
        barChartThisMonthSales.setOption(optionThisMonthSales, true);

        optionAccumulatedSales.xAxis[0].data = json.accumulatedSales.times;
        optionAccumulatedSales.series[0].data = json.accumulatedSales.values;
        barChartAccumulatedSales.setOption(optionAccumulatedSales, true);

        optionThisMonthStock.xAxis[0].data = json.thisMonthStock.times;
        optionThisMonthStock.series[0].data = json.thisMonthStock.values;
        barChartThisMonthStock.setOption(optionThisMonthStock, true);
        jQuery('#myModal').modal('show');
    });
}


function getDistributorMonthlyReportStats(distributorID, year, month)
{
    jQuery.ajaxSettings.async = false;
    var params = [];

    params.push('parameters[distributor_id]=' + distributorID );
    params.push('parameters[year]=' + jQuery("select[name='year']").val());
    params.push('parameters[month]=' + jQuery("select[name='month']").val());

    jQuery.getJSON(nonghuaBaseUrl + '/api/sales_monthly_for_distributor?' + params.join('&'), function (json) {
        optionThisMonthDelivery.xAxis[0].data = json.thisMonthDelivery.times;
        optionThisMonthDelivery.series[0].data = json.thisMonthDelivery.values;
        barChartThisMonthDelivery.setOption(optionThisMonthDelivery, true);

        optionThisMonthSales.xAxis[0].data = json.thisMonthSales.times;
        optionThisMonthSales.series[0].data = json.thisMonthSales.values;
        barChartThisMonthSales.setOption(optionThisMonthSales, true);

        optionAccumulatedSales.xAxis[0].data = json.accumulatedSales.times;
        optionAccumulatedSales.series[0].data = json.accumulatedSales.values;
        barChartAccumulatedSales.setOption(optionAccumulatedSales, true);

        optionThisMonthStock.xAxis[0].data = json.thisMonthStock.times;
        optionThisMonthStock.series[0].data = json.thisMonthStock.values;
        barChartThisMonthStock.setOption(optionThisMonthStock, true);
    });
}

function getjurismonthlyStats(){
    jQuery.ajaxSettings.async = false;
    var params = [];
    if(jQuery("select[name='juris']").val() == ""){
        alert('区域为必选！'); return false;
    }
    params.push('parameters[distributor]=' + jQuery("select[name='distributor']").val());
    params.push('parameters[juris]=' + jQuery("select[name='juris']").val());
    params.push('parameters[salesman]=' + jQuery("select[name='salesman']").val());
    params.push('parameters[year]=' + jQuery("select[name='year']").val());
    params.push('parameters[month]=' + jQuery("select[name='month']").val());

    jQuery.getJSON(nonghuaBaseUrl + '/api/sales_monthly_by_zone?' + params.join('&'), function (json) {
        optionThisMonthDelivery.xAxis[0].data = json.thisMonthDelivery.times;
        optionThisMonthDelivery.series[0].data = json.thisMonthDelivery.values;
        barChartThisMonthDelivery.setOption(optionThisMonthDelivery, true);

        optionThisMonthSales.xAxis[0].data = json.thisMonthSales.times;
        optionThisMonthSales.series[0].data = json.thisMonthSales.values;
        barChartThisMonthSales.setOption(optionThisMonthSales, true);

        optionAccumulatedSales.xAxis[0].data = json.accumulatedSales.times;
        optionAccumulatedSales.series[0].data = json.accumulatedSales.values;
        barChartAccumulatedSales.setOption(optionAccumulatedSales, true);

        optionThisMonthStock.xAxis[0].data = json.thisMonthStock.times;
        optionThisMonthStock.series[0].data = json.thisMonthStock.values;
        barChartThisMonthStock.setOption(optionThisMonthStock, true);
        jQuery('#myModal').modal('show');
    });
}
/*******************************图表相关************************************/
/* 柱状图、参数 */
var optionThisMonthDelivery = {
    show : true,
    tooltip : {
        trigger: 'axis'
    },
    legend: {
        data:['当月发货']
    },
    toolbox: {
        show : false
    },
    color : [ '#00a0a0' ],
    calculable : false,
    xAxis : [
        {
            type : 'category',
            data : []
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name: '当月发货',
            type: 'bar',
            barWidth: 30,
            data: []
        }
    ]
};
var optionThisMonthSales = {
    show : true,
    tooltip : {
        trigger: 'axis'
    },
    legend: {
        data:['当月销售']
    },
    toolbox: {
        show : false
    },
    color : [ '#00a0a0' ],
    calculable : false,
    xAxis : [
        {
            type : 'category',
            data : []
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name: '当月销售',
            type: 'bar',
            barWidth: 30,
            data: []
        }
    ]
};
var optionAccumulatedSales = {
    show : true,
    tooltip : {
        trigger: 'axis'
    },
    legend: {
        data:['累计销售']
    },
    toolbox: {
        show : false
    },
    color : [ '#00a0a0' ],
    calculable : false,
    xAxis : [
        {
            type : 'category',
            axisLabel: {
                interval: 0
            },
            data : []
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name: '累计销售',
            type: 'bar',
            barWidth: 30,
            data: []
        }
    ]
};
var optionThisMonthStock = {
    show : true,
    tooltip : {
        trigger: 'axis'
    },
    legend: {
        data:['库存数量']
    },
    toolbox: {
        show : false
    },
    color : [ '#00a0a0' ],
    calculable : false,
    xAxis : [
        {
            type : 'category',
            data : []
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name: '库存数量',
            type: 'bar',
            barWidth: 30,
            data: []
        }
    ]
};

var barChartThisMonthDelivery = echarts.init(document.getElementById('divThisMonthDelivery'));
var barChartThisMonthSales = echarts.init(document.getElementById('divThisMonthSales'));
var barChartAccumulatedSales = echarts.init(document.getElementById('divAccumulatedSales'));
var barChartThisMonthStock = echarts.init(document.getElementById('divThisMonthStock'));

jQuery(function($) {
    initMap("#map_previous", "#map_next");
    $(".content").addClass('map');
    $("#block-system-main").addClass('map');
    $("#map").button("toggle");
    $("#zone-wrapper-div").hide();


    $("#map").click(function (e) {
        e.preventDefault();
        $("#zone-wrapper-div").slideUp();
        $("#map-wrapper-div").slideDown();
        $(".content").addClass('map');
        $(".content").removeClass('zone');
        $("input[name='map_or_zone']").val('map');
        $("#monthlyStat").show();
        $("#jurismonthlyStat").hide();
    });

    $("#zone").click(function (e) {
        e.preventDefault();
        $("#map-wrapper-div").slideUp();
        $("#zone-wrapper-div").slideDown();
        $(".content").addClass('zone');
        $(".content").removeClass('map');
        $("input[name='map_or_zone']").val('zone');
        $("#monthlyStat").hide();
        $("#jurismonthlyStat").attr("style","display:block;clear:both");
    });
});
